package com.qbw.app.module.common.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.qbw.app.R;
import com.qbw.app.databinding.ActivityWebviewBinding;
import com.qbw.app.module.common.fragment.WebViewFragment;
import com.qbw.core.base.BaseActivity;
import com.qbw.customview.titlebar.TitleBar;

/**
 * Created by qbw on 2018/4/4.
 */

public class WebViewActivity extends BaseActivity<ActivityWebviewBinding> implements TitleBar.Listener {

    public static final String KEY_TITLE = "key_title";
    public static final String KEY_URL = "key_url";
    public static final String KEY_FLAG = "key_flag";
    public static final String KEY_PHONE = "key_phone";

    private String mTitle;
    private String mUrl;

    private int mFlag;
    private String mPhone;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview;
    }

    public static void start(Context context, String title, String url, int flag, String phone) {
        Intent intent = new Intent(context, WebViewActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra(KEY_TITLE, title);
        intent.putExtra(KEY_URL, url);
        intent.putExtra(KEY_FLAG, flag);
        intent.putExtra(KEY_PHONE, phone);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = savedInstanceState == null ? getIntent().getExtras() : savedInstanceState;
        mTitle = args.getString(KEY_TITLE);
        mUrl = args.getString(KEY_URL);
        mFlag = args.getInt(KEY_FLAG);
        mPhone = args.getString(KEY_PHONE);
        mBinding.titlebar.setTitle(mTitle);
        mBinding.titlebar.setListener(this);
        WebViewFragment fragment = WebViewFragment.createInstance(mUrl, mFlag, mPhone);
        getSupportFragmentManager().beginTransaction()
                                   .add(R.id.container, fragment)
                                   .commitAllowingStateLoss();
    }

    @Override
    public void onLeftAreaClick() {
        finish();
    }

    @Override
    public void onRightAreaClick() {

    }

    @Override
    public void onCenterAreaClick() {

    }

    public void onReceiveTitle(String title) {
        if (TextUtils.isEmpty(mTitle)) {
            mBinding.titlebar.setTitle(title);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_TITLE, mTitle);
        outState.putString(KEY_URL, mUrl);
        outState.putInt(KEY_FLAG, mFlag);
        outState.putString(KEY_PHONE, mPhone);
    }
}
